package com.mars.algorithm;

/**
 * @Author King
 * @create 2020/8/5 9:38
 */
public class Temp3 {


    public static void main(String[] args) {
        int[] ss = new int[]{78, 45, 13, 90, 56, 34, 65, 67};
        mSort(ss);
        int[] s = new int[]{78, 45, 13, 90, 56, 34, 65, 67};
        insertSort(s);
        int[] sss = new int[]{78, 45, 13, 90, 56, 34, 65, 67};
        quickSort(sss, 0, ss.length - 1);
        System.out.println("sdfgd");
    }

    private static void mSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            boolean flag = false;
            for (int j = 0; j < arr.length - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = tmp;
                    flag = false;
                }
                flag = true;
            }
            if (flag) {
                return;
            }
        }
    }

    private static void insertSort(int[] arr) {

        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = tmp;
        }

    }


    private static void quickSort(int[] arr, int left, int right) {
        if (null == arr || arr.length <= 1 || left > right) {
            return;
        }
        int mid = partition(arr, left, right);
        quickSort(arr, left, mid - 1);
        quickSort(arr, mid + 1, right);
    }

    private static int partition(int[] arr, int left, int right) {
        int key = arr[left];
        while (left < right) {
            while (arr[right] >= key && left < right) {
                right--;
            }
            arr[left] = arr[right];
            while (arr[left] <= key && left < right) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[right] = key;
        return right;
    }

}
